/*
  Copyright © 2010  Vinícius dos Santos Oliveira

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#ifndef DRAGGABLETOGGLEBUTTON_H
#define DRAGGABLETOGGLEBUTTON_H

#include <QGraphicsObject>
#include <QPen>

class DraggableToggleButton : public QGraphicsObject
{
    Q_OBJECT

    static QList<DraggableToggleButton *> m_buttons;

    QRectF m_boundingRect;
    QRectF m_paintBoundingRect;
    QPointF pressedAt;
    QPointF lastPos;
    bool checked;
    QPen pen;
    bool doubleClickEmitted;

    QColor pallete[2];

protected:
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);

public:
    DraggableToggleButton();
    ~DraggableToggleButton();

    virtual QRectF boundingRect() const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    virtual QPainterPath shape() const;

    bool isChecked() const;
    void setColor(const QColor &normal = QColor(240, 206, 17), const QColor &actived = QColor(248, 224, 20));

    static const QList<DraggableToggleButton *> &buttons();

public slots:
    void setChecked(bool);
    void toggle();
    void updateBoundingRect(const QRectF &boundingRect);

signals:
    void clicked();
    void pressed();
    void release();
    void toggled(bool checked);
    void dragged(DraggableToggleButton * = NULL);
    void doubleClicked();
};

inline void DraggableToggleButton::toggle()
{
    setChecked(!checked);
}

#endif // DRAGGABLETOGGLEBUTTON_H
